﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Jw.Web.Areas.Pacm.Models;
using System.Web.Http;
using Jw.Core;
using Jw.Models;
using Jw.Web.Areas.Mms.Common;
using Jw.Web.App_Start;
namespace Jw.Web.Areas.Pacm.Controllers
{
    public class mbjhController : BaseController
    {
        //
        // GET: /Pacm/mbjh/

        public ActionResult Index(string id)
        {
            ViewBag.M = id;
            //年份
            List<ComItem> comYearItems = new List<ComItem>();
            comYearItems.Add(new ComItem() { text = DateTime.Now.AddYears(-1).Year.ToString(), value = DateTime.Now.AddYears(-1).Year.ToString() });
            comYearItems.Add(new ComItem() { text = DateTime.Now.Year.ToString(), value = DateTime.Now.Year.ToString() });
            comYearItems.Add(new ComItem() { text = DateTime.Now.AddYears(1).Year.ToString(), value = DateTime.Now.AddYears(1).Year.ToString() });
            //月份
            List<ComItem> comMonthItems = new List<ComItem>();
            for (int i = 0; i < 12; i++)
            {
                ComItem obj = new ComItem();
                obj.text = obj.value = (i + 1).ToString("00");
                comMonthItems.Add(obj);
            }
            //车间
            List<ComItem> comCjItems = new List<ComItem>();
            comCjItems.Add(new ComItem() { text = "电解一车间", value="电解一车间" });
            comCjItems.Add(new ComItem() { text = "电解二车间", value = "电解二车间" });
            comCjItems.Add(new ComItem() { text = "铸造车间", value = "铸造车间" });
            comCjItems.Add(new ComItem() { text = "组装车间", value = "组装车间" });

            var model = new
            {
                dataSource = new
                {
                    yearsItems = comYearItems,
                    monthItems = comMonthItems,
                    cjItems=comCjItems,
                    curYear = DateTime.Now.Year.ToString(),
                    curMonth = DateTime.Now.Month.ToString("00")
                },
                form = new
                {
                    Mbjh_Year = DateTime.Now.Date.Year,
                    Mbjh_Month = "",
                    Mbjh_UpdateDate = "",
                    Mbjh_cjItems=""
                }

            };
            return View(model);
        }

    }
    public class mbjhApiController : ApiController
    {
        public dynamic Get(RequestWrapper query)
        {
            query.LoadSettingXmlString(@"
            <settings defaultOrderBy='Zlmbjh_BillNo'>
                <select>
                   p_Zlmbjh.*
                </select>
                <from>
                    p_Zlmbjh
                </from>
                <where defaultForAll='true' defaultCp='equal' defaultIgnoreEmpty='true' >
                    <field name='Zlmbjh_BillNo'             cp='equal'     ></field>
                </where>
            </settings>
            ");
            var pQuery = query.ToParamQuery();
            var list = new p_ZlmbjhService().GetDynamicListWithPaging(pQuery);
            return list;

        }
        public string GetNewBillNo()
        {
            return new p_ZlmbjhService().GetNewKey("Zlmbjh_BillNo", "dateplus");
        }
        public dynamic GetEditMaster(string id)
        {
            p_Zlmbjh obj = new p_ZlmbjhService().GetModel(ParamQuery.Instance().AndWhere("Zlmbjh_BillNo", id));
            return new
            {
                form = obj,
                scrollKeys = new p_ZlmbjhService().ScrollKeys("Zlmbjh_BillNo", id.ToString())
            };
        }
        public void Edit(dynamic data)
        {
            var listWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
            <settings>
                <table>{0}</table>
                <where><field name='Zlmbjh_BillNo' cp='equal'></field></where>
            </settings>", typeof(p_Zlmbjh).Name);

            var result = new p_ZlmbjhService().Edit(null,listWrapper,data);
        }

        public void Delete(string id)
        {
            var result = new p_ZlmbjhService().Delete(ParamDelete.Instance().AndWhere("Zlmbjh_BillNo", id));
            MmsHelper.ThrowHttpExceptionWhen(result <= 0, "单据删除失败[Zlmbjh_BillNo={0}]，请重试或联系管理员！", id);
        }
    }
}
